package com.foreknow.mapping;

import java.util.HashMap;
import java.util.Map;

public class MappingFactory {
    /**
     * 不允许每个人都new一下,节省内存空间,所以我们需要使用单例模式;
     * 私有构造器,提供一个静态方法,私有的属性
     * 用来存储对象,我们需要先创建一个容器;
     * 静态的方法无法访问成员变量;可以访问静态变量.
     * 定义常量充当key
     * 把定义过程和实例化过程分离开
     */
    private Map<String, EntityMapping> map = new HashMap<>();
    private static MappingFactory mappingFactory = null;
    //方便调用,类名点,不要实例化,节省内存,后面还有地方需要用到.
    public static final String USERS_MAPPING = "usersmapping";
    public static final String GUESTBOOK_MAPPING = "gusetbookmapping";
    private MappingFactory(){

    }
    public static MappingFactory getmf(){
       if (mappingFactory==null){
           mappingFactory = new MappingFactory();
       }
//       EntityMapping e1 = new UsersMapping();
//       EntityMapping e2 = new GuestbookMapping();
        //省略两行代码,向集合中存元素
       mappingFactory.map.put(USERS_MAPPING,new UsersMapping());
       mappingFactory.map.put(GUESTBOOK_MAPPING,new GuestbookMapping());
       return mappingFactory;
    }
// 定义取元素的方法,在集合中取.就是一个比较大的大家庭
    public EntityMapping getmapping(String key){
        return map.get(key);
    }



    public Map<String, EntityMapping> getMap() {
        return map;
    }

    public void setMap(Map<String, EntityMapping> map) {
        this.map = map;
    }


}
